<template>
  <view class="cart">
    <!-- 1 列表 -->

    <view class="cart-list">
      <u-swipe-action
        :show="item.show"
        :index="item.goods_id"
        v-for="item in goodList"
        :key="item.goods_id"
        @click="handleClick"
        :options="options"
        @open="handelChangeShow"
      >
        <view class="cart-item">
          <!-- 1 复选框 -->
          <view class="goods-chk">
            <u-checkbox
              :value="item.checked"
              shape="circle"
              active-color="red"
              @change="changeSelected(item.goods_id)"
            ></u-checkbox>
          </view>
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2"> {{ item.goods_name }} </view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view
                  class="num-btn"
                  @click="changeCount(item.goods_id, -1, item.count)"
                  >-</view
                >
                <view class="goods-num">{{ item.count }}</view>
                <view
                  class="num-btn"
                  @click="changeCount(item.goods_id, 1, item.count)"
                  >+</view
                >
              </view>
            </view>
          </view>
        </view>
      </u-swipe-action>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-chk">
        <u-checkbox
          shape="circle"
          active-color="red"
          :value="checkedAll"
          @change="selectedAll(checkedAll)"
          >全选</u-checkbox
        >
      </view>
      <view class="all-price">
        合计: <text>￥{{ goodsTotalPrice }}</text>
      </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="handleToPay"
          >去结算({{ goodsTotalPrice }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  data () {
    return {
      disabled: false,
      btnWidth: 180,
      show: false,
      options: [
        {
          text: '收藏',
          style: {
            backgroundColor: '#007aff'
          }
        },
        {
          text: '删除',
          style: {
            backgroundColor: '#dd524d'
          }
        }
      ],
      // 统一控制 滑动隐藏
      showSwipe: true
    }
  },
  computed: {
    ...mapState('cart', ['goodList']),
    ...mapGetters('cart', ['checkedAll', 'goodsTotalPrice', 'checkedList'])
  },
  onHide () {
    this.cartGoodShow()
  },
  methods: {
    ...mapMutations('cart', ['changeChecked', 'changeAll', 'changeGoodsCount', 'delGoods', 'changeShow', 'closeCartGoodShow']),
    changeSelected (id) {
      this.changeChecked({ id })
    },
    selectedAll (checkedAll) {
      this.changeAll({ checkedAll: !checkedAll })
    },
    async changeCount (id, unit, count) {
      if (unit === -1 && count === 1) {
        const [err, { confirm }] = await uni.showModal({
          title: '提示',
          content: '您是否要删除该商品',
        })
        console.log(confirm)
        if (confirm) {
          this.delGoods(id)
        }
      } else {
        this.changeGoodsCount({ id, unit })
      }
    },
    handleClick (id, index1) {
      // 区分用户点击的是收藏还是删除
      //  1. 第一个参数 表示 传递参数 
      //  2. 第二个参数 表示 点击是收藏 还是删除 对应所在数组的下标
      console.log(id, index1)
      if (index1 === 1) {
        this.delGoods(id)
      }
    },
    handelChangeShow (index) {
      // 获取到商品的id
      console.log(index)
      // 把id传递给mutations 修改对应商品的show状态
      this.changeShow(index)
    },
    cartGoodShow () {
      this.closeCartGoodShow()
    },
    handleToPay () {
      if (this.checkedList.length > 0) {
        uni.navigateTo({
          url: '/pages/pay/pay'
        })
      } else {
        uni.showModal({
          title: '提示',
          content: '请添加商品至购物车',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }

    }
  },
};
</script>

<style lang="scss">
.cart {
  padding-bottom: 111rpx;
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        &active {
        }
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: #eb4450;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: space-between;
            .num-btn {
              border: 1rpx solid #ccc;
              border-radius: 50%;
              width: 40rpx;
              height: 40rpx;
              text-align: center;
              line-height: 40rpx;
            }
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;
    .all-chk {
    }

    .all-price {
      flex: 1;
      color: #666;
      text {
      }
    }

    .all-count {
    }
  }
}
</style>